<?php

define('BASE_DIR', '../../');

ini_set('display_errors', 'on');
ini_set('memory_limit', '128M');
error_reporting(E_ALL);

require_once('../../vendor/Zend/Registry.php');

Zend_Registry::set('DISABLE_ACL', true);
Zend_Registry::set('DISABLE_MODULE_LOADER', true);
Zend_Registry::set('DISABLE_SITE_SWITCHER', true);

require_once(BASE_DIR . 'lib/bootstrap.php');

ob_start();

$migration = new Doctrine_Migration(BASE_DIR . 'db/migrations');

try {
    if ($migration->getCurrentVersion() > 0){
	echo 'Resetting database<br/>';
	$migration->migrate(0);
    }
} catch (Exception $x) {
  echo $x->getMessage() . '<br/>';
}

try {
    echo 'Migrating to latest version<br/>';
    $migration->migrate();
} catch (Exception $x) {
  echo $x->getMessage() . '<br/>';
}

echo 'Creating roles<br/>';

$Roles = new Silk_Roles();

$Roles->create(array('name' => 'Guest'), true);
$Roles->create(array('name' => 'Administrator'), true);

echo 'Creating admin user<br/>';

$Users = new Silk_Users();

$admin = $Users->create(array('username' => 'admin', 'password' => Silk::EncryptPassword(DEFAULT_ADMIN_PASSWORD), 'status' => USER_STATUS_ACTIVE, 'role' => 'Administrator'), true);

$Sites = new Silk_Sites();

$default_site = $Sites->create(array('name' => 'Default Site', 'domain' => 'silkcms'), true);

Zend_Registry::set('DISABLE_MODULE_LOADER', false);

$moduleLoader = FP_Application_Subsystem::Factory('module_loader', null, 'Silk_Application_Subsystem_');

$Types = new Silk_Types();

$LayoutType = $Types->fetchByName('Layout Template', true);
$PageType   = $Types->fetchByName('Page Template', true);

$Objects = new Silk_Objects();

$default_layout = $Objects->create(
    array(
	'type_id' => $LayoutType->type_id,
	'site_id' => $default_site->site_id,
	'user_id' => $admin->user_id,
	'status'  => OBJECT_STATUS_PUBLISHED,
	'name'    => 'Default',
	'mimetype' => 'text/html',
	'revision' => 0,
	'content' => '
	    <div class="sample-container">
		<div class="sample-header">
		    <h1 tal:content="silk/site/title|default">You site title here</h1>
		</div>
		<div class="sample-content floatwrapper">
		    <div class="sample-page-content">
			<tal:block tal:content="structure page_content"/>
		    </div>
		    <div class="sample-links">
			<h5 i18n:translate="string:What now?">What now?</h5>
			<ul>
			    <li><a tal:attributes="href php:this.link(\'dashboard\')" i18n:translate="string:Dashboard"></a></li>
			    <li><a href="http://code.google.com/p/silkcms" i18n:translate="string:Silk CMS Site"></a></li>
			</ul>
		    </div>
		</div>
		<div class="sample-footer">
		    <span>&copy;2010</span><span tal:content="silk/company|default">Your company here</span>
		</div>
	    </div>
	',
	'meta'    => array(
	    'Doctype' => 'XHTML 1.0 Transitional',
	    'Custom Head' => '<link rel="stylesheet" type="text/css" href="/assets/styles/admin.css"/>'
	)
    ), true
);

$default_index = $Objects->create(
    array(
	'type_id' => $PageType->type_id,
	'site_id' => $default_site->site_id,
	'user_id' => $admin->user_id,
	'status'  => OBJECT_STATUS_PUBLISHED,
	'name'    => 'Home',
	'mimetype' => 'text/html',
	'revision' => 0,
	'content' =>
'
<h1>Hello World</h1>
<p id="sample-p1" class="silk-editable">If you are reading this than you have correctly set up Silk CMS</p>
',
	'meta' => array(
	    'Meta Title' => 'Home',
	    'Layout' => $default_layout->object_id
	)
    ), true
);

$Settings = new Silk_Settings();
$Settings->setValue('Default Type', 'Page Template', $default_site->site_id);
$Settings->setValue('Default Id', $default_index->object_id, $default_site->site_id);

echo 'Reset complete';

ob_end_flush();

?>